// pages/register/register.js
var publicFun = require('../../../utils/publicFun.js');
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    showPassword: false,
    phoneNum: '',
    password: '',
    code: '',
    message: '',
    captchaTime: '获取验证码',
    isDisabled : false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // this.getLoginCode();
  },
  /*
   * 点击绑定领奖
   */
  handleClickBindPhone: function (e) {
    var reqArr = [
      ['phoneNum', '请输入电话号码'],
      ['code', '请输入短信验证码']
    ];
    var validation = publicFun.requiredFun(reqArr, this.data);
    if (validation.state) {
      if (!publicFun.regularPhone(this.data.phoneNum)) {
        this.toastShow('请输入合法的电话号码');
        return
      }
      //开始发起请求
      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=phone&act=bind',
        data: {
          phone: this.data.phoneNum,
          verify_code: this.data.code,
        },
        success: (res) => {
          //请求成功
          this.toastShow('绑定成功');
          //2秒后返回到上一页
          setTimeout(() => {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        },
        fail: (error) => {
          this.toastShow(error);
          var t = setTimeout(()=>{
            this.setData({
              code: ''
            });
            clearTimeout(t);
          },2000)
        }
      })

    } else {
      this.toastShow(validation.msg);
    }
  },


  /*
  * 发送获取验证码
  */
  getVerifyCode: function () {
    let reqArr = [
      ['phoneNum', '请输入手机号码']
    ];
    var validation = publicFun.requiredFun(reqArr, this.data);
    if (validation.state) {

      if (!publicFun.regularPhone(this.data.phoneNum)) {
        this.toastShow('请输入合法的电话号码');
        return
      }

      var time = 60;
      this.setData({
        isDisabled: true
      })
      var captchaInterval = setInterval(() => {
        if (time < 1) {
          clearInterval(captchaInterval);
          this.setData({
            captchaTime: '获取验证码',
            isDisabled: false
          })
          return;
        }
        time--;
        this.setData({
          captchaTime: time + 's'
        })
      }, 1000);

      //发起请求
      app.Ajax({
        method: 'POST',
        url: '/api/index.php?app=phone&act=verifyCode',
        data: {
          phone: this.data.phoneNum
        },
        success: (res) => {
          //请求成功
          this.toastShow('已发送，请注意查收');
        },
      })
    } else {
      this.toastShow(validation.msg);
    }

  },

  /*
  * 绑定表单电话号码的值
  */
  bindPhoneInput: function (e) {
    this.setData({
      phoneNum: e.detail.value
    })
  },

  /*
  * 绑定表单密码的值
  */
  bindPasswordInput: function (e) {
    this.setData({
      password: e.detail.value
    })
  },

  /*
  * 绑定表单密码的值
  */
  bindCodeInput: function (e) {
    this.setData({
      code: e.detail.value
    })
  },

  /**
   * 显示密码
   */
  passwordShow: function () {
    this.setData({
      showPassword: true
    })
  },

  /**
   * 隐藏密码
   */
  passwordHide: function () {
    this.setData({
      showPassword: false
    })
  },

  /**
   * 获取用户登录码
   */
  getLoginCode: function () {
    wx.login({
      success: (res) => {
        this.code = res.code
      }
    });
  },

  /**
   * 微信自动绑定
   */
  handleWeixinBindPhone: function (e) {
    console.log(e)
    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=wechat&act=auto_bind_mobile',
      data: {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
      },
      success: (res) => {
        //请求成功
        this.toastShow('绑定成功');
        //2秒后返回到上一页
        setTimeout(() => {
          wx.navigateBack({
            delta: 1
          })
        }, 2000)
      },
      fail: (err) => {
        //请求成功
        this.toastShow('绑定失败');
        // this.getLoginCode();
      }
    })
    // console.log(e)
  },

  /**
  * 显示报错信息
  */
  toastShow: function (message) {
    this.setData({
      message: message,
    })
  },
})